package com.bbs.crawl;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.bbs.dao.ColorDaoOfHome;

public class CarForumCrawlOfAutoHome  {
	private ColorDaoOfHome bbsDao;

	static Logger logger = Logger.getLogger(CarForumCrawlOfAutoHome.class.getName());

	public void getColorForum(int carid) {
		logger.info("ColorModel:carID=" + carid + " Start");
		Document doc = null;
		try {
			doc = Jsoup.connect("http://www.autohome.com.cn/" + carid).get();

			Elements elements1 = doc.select(".car-color-pop .tip-content");
			if (elements1.size() == 0) {
				logger.info("不存在carID=" + carid + "的车型");
			} else {

				if (elements1.size() >= 1) // 如果存在更多颜色，打印更多颜色
				{
					for (Element color : elements1) {
						bbsDao.isInsert(color.text());
					}
				} else {

					elements1 = doc.select(".car-color  .tip-content");// 否则打印颜色

					for (Element color : elements1) {
						bbsDao.isInsert(color.text());
					}

				}

			}
			logger.info("ColorModel:carID=" + carid + " Finish");
		} catch (IOException e) {
			logger.error("carID=" + carid + " 无颜色");
		}
	}

	public CarForumCrawlOfAutoHome(ColorDaoOfHome bbsDao) {
		super();
		this.bbsDao = bbsDao;
	}


}